package com.tailyou.hms.firstapp.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.tailyou.hms.firstapp.R;
import com.tailyou.hms.firstapp.act.PaginationNavigatorActivity;

import java.util.List;

public abstract class PaginationAdapter extends RecyclerView.Adapter<PaginationAdapter.RVHolder> {

    private List<String> datas;

    public PaginationAdapter(List<String> datas) {
        this.datas = datas;
    }

    @NonNull
    @Override
    public PaginationAdapter.RVHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout_pagination, parent, false);
        return new RVHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PaginationAdapter.RVHolder holder, int position) {
        String text = datas.get(position);
        switch (text) {
            case "q":
            case "h":
                holder.tvPage.setText("...");
                break;
            default:
                holder.tvPage.setText(text);
                break;
        }
        if (text == String.valueOf(PaginationNavigatorActivity.curPage)) {
            holder.tvPage.setBackgroundColor(Color.argb(255, 155, 155, 155));
        } else {
            holder.tvPage.setBackgroundColor(Color.argb(0, 255, 255, 255));
        }
        holder.tvPage.setOnClickListener(v -> clickItem(holder.tvPage.getText().toString()));
    }

    public abstract void clickItem(String text);

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class RVHolder extends RecyclerView.ViewHolder {
        private TextView tvPage;

        public RVHolder(@NonNull View itemView) {
            super(itemView);
            tvPage = itemView.findViewById(R.id.tvPage);
        }
    }
}
